/* JSDAS
* Copyright (C) 2008-2009 Bernat Gel
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

var getSequence = function(url) {
	JSDAS.Demo.setStatus("Loading Sequence...", false);
	
	JSDAS.sequence(url, analyzeSequence, function(){alert("error")});
}

var analyzeSequence = function(response) {
	JSDAS.Demo.setStatus("Processing...", false);
	var seq = response.SEQUENCE[0].textContent; //get the first sequence from the response
	
	//We'v got the sequence and we're done with DAS. Now, just do something with it
	
	var sequence_div = document.getElementById('sequence');
	var densities_div = document.getElementById('densities');
	
	//write the sequence
	sequence_div.innerHTML = '<p>SEQUENCE: '+seq+'</p>';
	
	var l=seq.length;
	var densities = {};
	var twomers = {a: {a: 0, c: 0, t:0, g:0},
				   c: {a: 0, c: 0, t:0, g:0},
				   t: {a: 0, c: 0, t:0, g:0},
				   g: {a: 0, c: 0, t:0, g:0}};
	densities[seq[0]] = (densities[seq[0]])?(densities[seq[0]] + 1):1;
	for(var i=1; i<l; ++i) {
		densities[seq[i]] = (densities[seq[i]])?(densities[seq[i]] + 1):1;
		twomers[seq[i-1]][seq[i]]++;
	}
	
	var html = "<h3>Densities</h3><br>"; 
	for(base in densities) {
		html += "Base "+base+": "+densities[base]+"/"+l+" ("+100.0*(densities[base]/l)+"%)<br>";
	}
	html += '<br><br><h3>2-mers</h3><br>';
	html += '<table><tr><td></td><td>A</td><td>T</td><td>C</td><td>G</td></tr>';
	html += '<tr><td>A</td><td>'+twomers.a.a+' ('+100.0*(twomers.a.a/(l-1))+'%)</td><td>'+twomers.a.t+' ('+100.0*(twomers.a.t/(l-1))+'%)</td><td>'+twomers.a.c+' ('+100.0*(twomers.a.c/(l-1))+'%)</td><td>'+twomers.a.g+' ('+100.0*(twomers.a.g/(l-1))+'%)</td></tr>';
	html += '<tr><td>T</td><td>'+twomers.t.a+' ('+100.0*(twomers.t.a/(l-1))+'%)</td><td>'+twomers.t.t+' ('+100.0*(twomers.t.t/(l-1))+'%)</td><td>'+twomers.t.c+' ('+100.0*(twomers.t.c/(l-1))+'%)</td><td>'+twomers.t.g+' ('+100.0*(twomers.t.g/(l-1))+'%)</td></tr>';
	html += '<tr><td>C</td><td>'+twomers.c.a+' ('+100.0*(twomers.c.a/(l-1))+'%)</td><td>'+twomers.c.t+' ('+100.0*(twomers.c.t/(l-1))+'%)</td><td>'+twomers.c.c+' ('+100.0*(twomers.c.c/(l-1))+'%)</td><td>'+twomers.c.g+' ('+100.0*(twomers.c.g/(l-1))+'%)</td></tr>';
	html += '<tr><td>G</td><td>'+twomers.g.a+' ('+100.0*(twomers.g.a/(l-1))+'%)</td><td>'+twomers.g.t+' ('+100.0*(twomers.g.t/(l-1))+'%)</td><td>'+twomers.g.c+' ('+100.0*(twomers.g.c/(l-1))+'%)</td><td>'+twomers.g.g+' ('+100.0*(twomers.g.g/(l-1))+'%)</td></tr>';
	html += '</table>';
	
	//check base density in 10
	html += '<br><br><h3>Density Color-coded</h3><br>';
	for(i=0; i<l; i +=10) {
		var seq10 = seq.substr(i, 10);
		var density10 = {a: 0, t:0, c: 0, g:0 };
		for(var j=0, l10 = seq10.length; j<l10; ++j) {
			++density10[seq10[j]];
		}
		html += '<span style="color: '+getColor(density10, l10)+';" class="sequence">'+seq10+'</span>';	
	}
	html +='<br>';

	densities_div.innerHTML = html;	
	JSDAS.Demo.setStatus("Done");
}

var getColor= function(density, num) {
	var red = density.a/num*255.0;
	var green = density.t/num*255.0;
	var blue = density.c/num*255.0;
	//yellow
	red += density.g/num*255.0;
	green += density.g/num*255.0;
	return 'rgb('+parseInt(red)+', '+parseInt(green)+', '+parseInt(blue)+')';
}
